package com.ng.common.handler;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.jfinal.kit.JsonKit;
import com.ng.common.exception.ExpiredTokenException;
import com.ng.common.model.response.R;

import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public ResponseEntity<String> handleAllExceptions(Exception e) {
		R<String> r = new R<String>();
		r.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
		r.setMsg("系统异常:" + e.getMessage());
		return new ResponseEntity<>(JsonKit.toJson(r), HttpStatus.INTERNAL_SERVER_ERROR);
	}
	
	@ExceptionHandler(ExpiredTokenException.class )
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public ResponseEntity<String> handleExpiredTokenExceptions(Exception e) {
		R<String> r = new R<String>();
		r.setCode(HttpStatus.UNAUTHORIZED.value());
		r.setMsg("Unauthorized.");
		return new ResponseEntity<>(JsonKit.toJson(r), HttpStatus.UNAUTHORIZED);
	}
	
	@ExceptionHandler(ExpiredJwtException.class )
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public ResponseEntity<String> handleExpiredJwtExceptions(Exception e) {
		R<String> r = new R<String>();
		r.setCode(HttpStatus.UNAUTHORIZED.value());
		r.setMsg("Unauthorized.");
		return new ResponseEntity<>(JsonKit.toJson(r), HttpStatus.UNAUTHORIZED);
	}
	

}